package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.*;
import com.cskaoyan.market.vo.WxHomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WxHomeServiceImpl implements WxHomeService {
    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketCouponMapper marketCouponMapper;

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketAdMapper marketAdMapper;

    @Autowired
    MarketBrandMapper marketBrandMapper;

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Override
    public WxHomeVo index() {
        MarketGoodsExample newGoodsExample = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = newGoodsExample.createCriteria();
        criteria.andIsNewEqualTo(true);
        criteria.andDeletedEqualTo(false);
        List<MarketGoods> newGoodsList = marketGoodsMapper.selectByExampleSelective(newGoodsExample, MarketGoods.Column.id, MarketGoods.Column.name,
                MarketGoods.Column.brief, MarketGoods.Column.picUrl, MarketGoods.Column.isNew, MarketGoods.Column.isHot, MarketGoods.Column.counterPrice, MarketGoods.Column.retailPrice);
        // 限制 newGoodsList 的大小为 10 个
        if (newGoodsList.size() > 10) {
            newGoodsList = newGoodsList.subList(0, 10);
        }

        MarketCouponExample marketCouponExample = new MarketCouponExample();
        MarketCouponExample.Criteria marketCouponExampleCriteria = marketCouponExample.createCriteria();
        marketCouponExampleCriteria.andTypeEqualTo((short) 0);
        marketCouponExampleCriteria.andDeletedEqualTo(false);
        List<MarketCoupon> couponList = marketCouponMapper.selectByExampleSelective(marketCouponExample, MarketCoupon.Column.id, MarketCoupon.Column.name, MarketCoupon.Column.desc,
                MarketCoupon.Column.tag, MarketCoupon.Column.discount, MarketCoupon.Column.min, MarketCoupon.Column.days);

        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria marketCategoryExampleCriteria = marketCategoryExample.createCriteria();
        marketCategoryExampleCriteria.andPidEqualTo(0);
        marketCategoryExampleCriteria.andDeletedEqualTo(false);
        marketCategoryExampleCriteria.andLevelEqualTo("L1");
        marketCategoryExampleCriteria.andIconUrlIsNotNull();
        List<MarketCategory> channel = marketCategoryMapper.selectByExampleSelective(marketCategoryExample, MarketCategory.Column.id, MarketCategory.Column.name, MarketCategory.Column.iconUrl);

        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria marketAdExampleCriteria = marketAdExample.createCriteria();
        marketAdExampleCriteria.andDeletedEqualTo(false);
        marketAdExampleCriteria.andEnabledEqualTo(true);
        List<MarketAd> banner = marketAdMapper.selectByExampleSelective(marketAdExample, MarketAd.Column.id, MarketAd.Column.name, MarketAd.Column.link, MarketAd.Column.url,
                MarketAd.Column.position, MarketAd.Column.content, MarketAd.Column.enabled, MarketAd.Column.addTime, MarketAd.Column.updateTime, MarketAd.Column.deleted);

        MarketBrandExample marketBrandExample = new MarketBrandExample();
        marketBrandExample.createCriteria().andDeletedEqualTo(false);
        List<MarketBrand> brandList = marketBrandMapper.selectByExampleSelective(marketBrandExample, MarketBrand.Column.id, MarketBrand.Column.name, MarketBrand.Column.desc, MarketBrand.Column.picUrl, MarketBrand.Column.floorPrice);
        // 限制 brandList 的大小为 10 个
        if (brandList.size() > 10) {
            brandList = brandList.subList(0, 10);
        }

        MarketGoodsExample hotGoodsExample = new MarketGoodsExample();
        hotGoodsExample.createCriteria().andIsHotEqualTo(true);
        List<MarketGoods> hotGoodsList = marketGoodsMapper.selectByExampleSelective(hotGoodsExample, MarketGoods.Column.id, MarketGoods.Column.name,
                MarketGoods.Column.brief, MarketGoods.Column.picUrl, MarketGoods.Column.isNew, MarketGoods.Column.isHot, MarketGoods.Column.counterPrice, MarketGoods.Column.retailPrice);
        if (hotGoodsList.size() > 10) {
            hotGoodsList = hotGoodsList.subList(0, 10);
        }

        MarketTopicExample marketTopicExample = new MarketTopicExample();
        marketTopicExample.createCriteria().andDeletedEqualTo(false);
        List<MarketTopic> topicList = marketTopicMapper.selectByExampleSelective(marketTopicExample, MarketTopic.Column.id, MarketTopic.Column.title, MarketTopic.Column.subtitle,
                MarketTopic.Column.price, MarketTopic.Column.readCount, MarketTopic.Column.picUrl);

        Map<String, WxHomeVo.FloorGoods> floorGoodsMap = new HashMap<>();
        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        marketGoodsExample.createCriteria().andDeletedEqualTo(false);
        List<MarketGoods> goodsList = marketGoodsMapper.selectByExampleSelective(marketGoodsExample, MarketGoods.Column.id, MarketGoods.Column.name, MarketGoods.Column.brief,
                MarketGoods.Column.picUrl, MarketGoods.Column.isNew, MarketGoods.Column.isHot, MarketGoods.Column.counterPrice, MarketGoods.Column.retailPrice, MarketGoods.Column.categoryId);

        // 按 categoryId 分类
        Map<Integer, List<MarketGoods>> goodsMap = goodsList.stream()
                .collect(Collectors.groupingBy(MarketGoods::getCategoryId));
        // 创建 FloorGoods 对象,并添加到 floorGoodsMap 中
        for (Map.Entry<Integer, List<MarketGoods>> entry : goodsMap.entrySet()) {
            Integer categoryId = entry.getKey();
            if (categoryId == 0) {
                // 跳过 categoryId 为 0 的情况
                continue;
            }

            // 通过 categoryId 查询二级类目,再查询一级类目得到类目名
            MarketCategory marketCategorySecond = marketCategoryMapper.selectByPrimaryKey(categoryId);
            MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(marketCategorySecond.getPid());
            String firstLevelCategoryName = marketCategory.getName();
            Integer cid = marketCategory.getId();

            // 创建 FloorGoods 对象,并添加到 floorGoodsMap 中
            WxHomeVo.FloorGoods floorGoods = floorGoodsMap. getOrDefault(firstLevelCategoryName, new WxHomeVo.FloorGoods());
            floorGoods.setName(firstLevelCategoryName);
            floorGoods.setId(cid);
            List<MarketGoods> marketGoodsList = floorGoods.getGoodsList();
            List<MarketGoods> goodsListGroupByCid = entry.getValue();
            if (goodsListGroupByCid.size() > 10) {
                goodsListGroupByCid = goodsListGroupByCid.subList(0, 10);
            }
            if (marketGoodsList == null) {
                marketGoodsList = goodsListGroupByCid;
            } else if (marketGoodsList.size() > 10) {
                continue;
            } else {
                marketGoodsList.addAll(goodsListGroupByCid);
            }
            floorGoods.setGoodsList(marketGoodsList);
            floorGoodsMap.put(firstLevelCategoryName, floorGoods);
        }
        // 将 floorGoodsMap 转换为 List
        List<WxHomeVo.FloorGoods> floorGoodsList = new ArrayList<>(floorGoodsMap.values());
        return new WxHomeVo(newGoodsList, couponList, channel, banner, brandList, hotGoodsList, topicList, floorGoodsList);
    }
}
